package com.gs.thunder.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/10/30 0030
 */
public class DateUtil {
    private static SimpleDateFormat ymdsdf = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat ymdhmssdf = new SimpleDateFormat("yyyyMMddhhmmss");

    public static Map<String, String> getYearMonth() {
        Calendar calendar = Calendar.getInstance();
        Map result = new HashMap();
        result.put("year", calendar.get(Calendar.YEAR));
        result.put("month", calendar.get(Calendar.MONTH) + 1<10?"0"+(calendar.get(Calendar.MONTH) + 1):(calendar.get(Calendar.MONTH) + 1));
        return result;
    }

    public static boolean equal(Map date1 ,Map date2){
        if(date1.get("year").equals(date2.get("year"))&&date1.get("month").equals(date2.get("month")))
        return true;
        else return false;
    }


    public static Map<String, String> getYearPreMonth() {
        Calendar calendar = Calendar.getInstance();
        Map result = new HashMap();
        if (calendar.get(Calendar.MONTH) == 0) {
            result.put("year", calendar.get(Calendar.YEAR)-1);
            result.put("month", 12);
        } else {
            result.put("year", calendar.get(Calendar.YEAR));
            result.put("month", calendar.get(Calendar.MONTH)<10?"0"+(calendar.get(Calendar.MONTH)):calendar.get(Calendar.MONTH));
        }
        return result;
    }


    public static Map<String, String> getYearMonthDay() {
        Calendar calendar = Calendar.getInstance();
        Date dt=calendar.getTime();
        String reStr = ymdsdf.format(dt);
        Map result = new HashMap();
        result.put("year",reStr.substring(0,4));
        result.put("month",reStr.substring(4,6));
        result.put("day",reStr.substring(6,8));
        return result;
    }

    public static String getYMDHMS() {
        Calendar calendar = Calendar.getInstance();
        Date dt=calendar.getTime();
        String reStr = ymdhmssdf.format(dt);
        return reStr;
    }

    public static Map<String, String> getYearMonthYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,0);
        calendar.add(Calendar.MONTH,0);
        calendar.add(Calendar.DAY_OF_YEAR,-1);//日期减1天
        Date dt=calendar.getTime();
        String reStr = ymdsdf.format(dt);
        Map result = new HashMap();
        result.put("year",reStr.substring(0,4));
        result.put("month",reStr.substring(4,6));
        result.put("day",reStr.substring(6,8));
        return result;
    }

    public static Map<String, String> getYearMonthDayBeforeYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,0);
        calendar.add(Calendar.MONTH,0);
        calendar.add(Calendar.DAY_OF_YEAR,-2);//日期减1天
        Date dt=calendar.getTime();
        String reStr = ymdsdf.format(dt);
        Map result = new HashMap();
        result.put("year",reStr.substring(0,4));
        result.put("month",reStr.substring(4,6));
        result.put("day",reStr.substring(6,8));
        return result;
    }


}
